package com.Renting.controller;

import com.Renting.dao.LoginDao;
import com.Renting.domain.RentingUser;
import com.Renting.service.ServiceGain;
import com.Renting.service.ServiceLogin;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;


@Controller
public class Login {

    @Resource
    private ServiceLogin service;
    @Resource
    private ServiceGain serviceGain;
    @Resource
    private LoginDao dao;

    //登录程序
    @PostMapping(value = "/in.do",produces = "text/html; charset=utf-8")
    public String RentingIn(RentingUser rentingUser, HttpSession session){
        boolean result= service.ServiceIn(rentingUser,session);
        if (result){
            serviceGain.getRunting(session);
            serviceGain.GetHint(session);
            return "/shop";
        }else {
            session.setAttribute("ErrorLogin","登录失败，账号或密码错误!");
            return "/Login";
        }



    }

    //注册程序
    @PostMapping(value = "/signin.do",produces = "text/html; charset=utf-8")
    public String Rentingsignin(RentingUser rentingUser,HttpSession session){
        //判断用户输入的账号是否已经存在
       RentingUser use= dao.verify(rentingUser);
        //判断用户的输入是否符合规定
        if(use!=null){
            session.setAttribute("ErrorLogin","账号已被占用!");
            return "/Signin";
        }else{
            if(rentingUser.getUsername().length()>=6&&rentingUser.getUsername().length()<=12){
                if (rentingUser.getPassword().length()>=6&&rentingUser.getPassword().length()<=15) {
                    service.ServiceSignin(rentingUser,session);
                    return "/Login";
                }else{
                    session.setAttribute("ErrorLogin","密码请在6-15位数字之间!");
                    return "/Signin";
                }
            }else {
                session.setAttribute("ErrorLogin","用户名请在6-12位数字之间!");
                return "/Signin";
            }

        }
    }

    //管理员的登录程序
    @PostMapping(value = "/AdminPassword.do",produces = "text/html; charset=utf-8")
    public String AdminPassword(RentingUser user,HttpSession session){
        if(user.getPassword().equals("10086")){
            serviceGain.GetRoomUser(session);

            session.setAttribute("admin-user","10086");
            session.removeAttribute("AdminHint");
            return "Admin-min";
        }else{
            session.setAttribute("ErrorLogin","密码错误!");
            return "Admin";
        }

    }
}
